// LoginLogController.java
package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.entity.LoginLog;
import com.aaa.vegetablemarketexpress.service.LoginLogService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.PageResult;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/loginLog")
@Log4j2
public class LoginLogController {

    @Resource
    private LoginLogService loginLogService;

    /**
     * 查询登录日志列表
     * @param page 分页信息和查询条件
     * @return 登录日志列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody(required = false) Page<LoginLog> page) {
        log.info("查询登录日志列表");

        // 处理分页参数
        if (page == null) {
            page = new Page<>();
        }

        // 设置默认分页参数
        if (page.getPageNo() == null || page.getPageNo() <= 0) {
            page.setPageNo(1);
        }
        if (page.getPageSize() == null || page.getPageSize() <= 0) {
            page.setPageSize(10);
        }

        // 获取查询条件
        LoginLog loginLog = page.getData();
        if (loginLog == null) {
            loginLog = new LoginLog();
        }

        // 查询所有符合条件的数据
        List<LoginLog> allList = loginLogService.selectLoginLogList(loginLog);

        // 实现分页
        int total = allList.size();
        int pageSize = page.getPageSize();
        int pageNum = page.getPageNo();

        int fromIndex = (pageNum - 1) * pageSize;
        // 确保fromIndex不超出列表范围
        fromIndex = Math.min(fromIndex, total);
        int toIndex = Math.min(fromIndex + pageSize, total);

        // 截取当前页数据
        List<LoginLog> pageList = fromIndex < toIndex ? allList.subList(fromIndex, toIndex) : new ArrayList<>();

        // 构建分页结果
        PageResult<LoginLog> pageResult = new PageResult<>(pageNum, pageSize, total, pageList);

        return ResultUtil.success(pageResult);
    }

    /**
     * 删除登录日志
     * @param loginId 登录日志ID
     * @return 删除结果
     */
    @DeleteMapping("/{loginId}")
    public Result remove(@PathVariable Long loginId) {
        log.info("删除登录日志，ID: {}", loginId);
        Long[] loginIdsArray = {loginId};
        int result = loginLogService.deleteLoginLogByIds(loginIdsArray);
        if (result > 0) {
            return ResultUtil.success("删除成功");
        } else {
            return ResultUtil.failed("删除失败");
        }
    }

    /**
     * 清空登录日志
     * @return 清空结果
     */
    @DeleteMapping("/clean")
    public Result clean() {
        log.info("清空登录日志");
        try {
            loginLogService.cleanLoginLog();
            return ResultUtil.success("清空登录日志成功");
        } catch (Exception e) {
            log.error("清空登录日志失败", e);
            return ResultUtil.failed("清空登录日志失败: " + e.getMessage());
        }
    }
}
